package com.ewing.busi.resource.service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.ewing.busi.resource.dao.WebResourceDao;
import com.ewing.busi.resource.dao.WebResourcePriceDao;
import com.ewing.busi.resource.dao.WebResourceSpecDao;
import com.ewing.busi.resource.dto.ProductSpec;
import com.ewing.busi.resource.dto.ProductSpecGroup;
import com.ewing.busi.resource.model.WebResourcePrice;
import com.ewing.busi.resource.model.WebResourceSpec;
import com.ewing.busi.resource.util.ProductSpecComparatorUtil;
import com.ewing.core.jdbc.BaseDao;
import com.ewing.utils.IntegerUtils;

/**
 * 资源规格服务类
 * 
 * @author tansonlam
 * @createDate 2016年1月27日
 * 
 */
@Repository("webResourceSpecService")
public class WebResourceSpecService {
  
  private static Logger logger = Logger.getLogger(WebResourceSpecService.class);


  @Resource
  private BaseDao baseDao;
  @Resource
  private WebResourceDao webResourceDao;
  @Resource
  private WebResourcePriceDao webResourcePriceDao;
  @Resource
  private WebResourceSpecDao webResourceSpecDao;

  /**
   * 获取配置的资源規格,以规格组的结构返回
   * 
   * @param resourceId
   * @return
   */
  public List<ProductSpecGroup> getConfigureSpecs(Integer resourceId) {
    List<ProductSpecGroup> groupList = new ArrayList<ProductSpecGroup>();
    List<WebResourceSpec> specList = webResourceSpecDao.getConfigureSpecs(resourceId);
    for (WebResourceSpec spec : specList) {
      if (!StringUtils.isEmpty(spec.getRootSpec()))
        continue;
      ProductSpec rootSpecDto = new ProductSpec();
      try {
        BeanUtils.copyProperties(rootSpecDto, spec);

        ProductSpecGroup group = new ProductSpecGroup();
        group.setRootSpec(rootSpecDto);
        for (WebResourceSpec childSpec : specList) {
          if (!StringUtils.isEmpty(childSpec.getRootSpec())
              && childSpec.getRootSpec().equals(spec.getSpec())) {
            ProductSpec childSpecDto = new ProductSpec();
            BeanUtils.copyProperties(childSpecDto, childSpec);
            group.addChildSpec(childSpecDto);
          }
        }
        groupList.add(group);
      } catch (IllegalAccessException e) {
        logger.error(e.getMessage(), e);
      } catch (InvocationTargetException e) {
        logger.error(e.getMessage(), e);
      }
    }
    ProductSpecComparatorUtil.sortResGroupList(groupList);
    return groupList;
  }

  public List<WebResourceSpec> getSpecByPriceId(Integer priceId) {
    if (IntegerUtils.nullOrZero(priceId)) {
      return Collections.emptyList();
    }

    WebResourcePrice price = baseDao.findOne(priceId, WebResourcePrice.class);
    if (null == price || StringUtils.isEmpty(price.getSpecIds())) {
      return Collections.emptyList();
    }

    String[] specIdStrs = StringUtils.split(price.getSpecIds(), ",");
    return baseDao.findMuti(IntegerUtils.toIntList(specIdStrs), WebResourceSpec.class);
  }
}
